package love.yuanmengrobot.qdkaoqin.business;

import android.content.Intent;
import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import love.yuanmengrobot.qdkaoqin.R;
import love.yuanmengrobot.qdkaoqin.adapter.KaoqinAdapter;
import love.yuanmengrobot.qdkaoqin.adapter.StudentKaoqinAdapter;
import love.yuanmengrobot.qdkaoqin.entity.AttendanceDate;
import love.yuanmengrobot.qdkaoqin.entity.StudentAttendanceDate;
import love.yuanmengrobot.qdkaoqin.util.TeacherConstants;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class StudentClassDescActivity extends AppCompatActivity {
    private String CLAZZ_ID;
    private String CLAZZ_NAME;
    private String MEMBER_ID;
    private String urlMemberidParams;
    private ListView kaoqinList;
    private List<StudentAttendanceDate> attendanceDates;
    private StudentKaoqinAdapter stuAdapter;

    private Button numberButton;
    private Button qrButton;
    private TextView tvClazzName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classes_student);
//        kaoqinList=findViewById(R.id.kaoqinList);
        Intent intent = getIntent();
        CLAZZ_ID = intent.getStringExtra(TeacherConstants.CLAZZ_ID);
        CLAZZ_NAME = intent.getStringExtra(TeacherConstants.CLAZZ_NAME);
        MEMBER_ID = intent.getStringExtra(TeacherConstants.MEMBERID);
        urlMemberidParams = "?memberid=" + MEMBER_ID+"&clazzid="+CLAZZ_ID;
        System.out.println("$班级id：" + CLAZZ_ID);
        System.out.println("$班级名称：" + CLAZZ_NAME);
        System.out.println("$用户id：" + MEMBER_ID);
        initView();
        initDate();

    }

    private void initView() {
        kaoqinList = findViewById(R.id.lv_kaoqinList);
        tvClazzName = findViewById(R.id.tv_clazz_name_student);
        tvClazzName.setText("班级：" + CLAZZ_NAME);
        numberButton=findViewById(R.id.bu_num_attendance);
        qrButton=findViewById(R.id.bu_qr_attendance);


        numberButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(StudentClassDescActivity.this,
                        StudentKaoqinActivity.class);
                intent.putExtra(TeacherConstants.CLAZZ_ID,
                        CLAZZ_ID);
                intent.putExtra(TeacherConstants.CLAZZ_NAME,
                        CLAZZ_NAME);
                intent.putExtra(TeacherConstants.MEMBERID,
                        MEMBER_ID);
                startActivity(intent);


            }
        });
//        newKaoqingButton = findViewById(R.id.bu_new_kaoqin);
//        newKaoqingButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //MEMBER_ID
//                //CLAZZ_ID
//                Intent intent = new Intent(KaoqinActivity.this,
//                        StartAttendanceActivity.class);
//                intent.putExtra(TeacherConstants.CLAZZ_ID,
//                        CLAZZ_ID);
////                intent.putExtra(TeacherConstants.CLAZZ_NAME,
////                        clazz.getClazzname());
//                intent.putExtra(TeacherConstants.MEMBERID,
//                        MEMBER_ID);
//                startActivity(intent);
//            }
//        });

    }
    public void initDate() {
        attendanceDates = new ArrayList<>();
        stuAdapter = new StudentKaoqinAdapter(StudentClassDescActivity.this,
                R.layout.kaoqin_info, attendanceDates);
        kaoqinList.setAdapter(stuAdapter);
        refreshData(1);
    }
    private okhttp3.Callback callback = new okhttp3.Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
//            Log.e(TAG, "Failed to connect server!");
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response)
                throws IOException {
            if (response.isSuccessful()) {
                final String body = response.body().string();
                System.out.println("67777777776:" + body);
                if(body==null)return;
                if(body.contains("失败")||body.length()<4)return;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        Type jsonType =////////****
                                new TypeToken<List<StudentAttendanceDate>>() {
                                }.getType();
                        List<StudentAttendanceDate> attendanceDateListResponse =
                                gson.fromJson(body, jsonType);
                        if(attendanceDateListResponse==null)return;
                        for (StudentAttendanceDate sat : attendanceDateListResponse) {
                            stuAdapter.add(sat);
                        }
                        stuAdapter.notifyDataSetChanged();
                    }
                });
            } else {

            }
        }
    };


    private void refreshData(final int page) {
//?memberid=11
        new Thread(new Runnable() {
            @Override
            public void run() {

                Request request = new Request.Builder()
                        .url(TeacherConstants.STU_KAOQIN_DESC_URL + urlMemberidParams)
                        .get().build();
                try {
                    OkHttpClient client = new OkHttpClient();
                    client.newCall(request).enqueue(callback);
                } catch (NetworkOnMainThreadException ex) {
                    ex.printStackTrace();
                }
            }
        }).start();
    }

}
